#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_WriteDataById_0x2E_test.py
@Time    :   2022/8/25 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume
 
import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


WriteDID = [        ('2E 34 01 01','6E 34 01','22 34 01','62 34 01 01'),                                                        # FactoryMode 生产模式
                    ('2E 21 00 10 A5 70 00 00 00 00 70','6E 21 00','22 21 00','62 21 00 10 A5 70 00 00 00 00 70'),              # Variant Coding 配置字
                    ('2E F1 A1 FF FF FF FF FF FF FF FF','6E F1 A1','22 21 00','62 21 00 00 00 00 00 00 00 00 00'),              # Variant Coding 配置字擦除
                    # ('2E 34 4A 00','6E 34 4A','',''),   
                    # ('2E 34 4A 01','6E 34 4A','',''),                                                                         # Lifecycle State Switch
                    ('2E F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF','6E F1 90','22 F1 90','62 F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF'),  # VehicleIdentification(VIN) 车辆识别码
            ]
WriteDID_NRC13 =[   ('2E','7F 2E 13','3'),  
                    ('2E 34','7F 2E 13','3'),
                    ('2E 34 01','7F 2E 13','3'), 
                    ('2E 34 01 01 01','7F 2E 13','3'),
                    ('2E 21 00','7F 2E 13','3'), 
                    ('2E 21 00 00 00 00 00 00 00 00','7F 2E 13','3'),                                
                    ('2E F1 A1','7F 2E 13','3'), 
                    ('2E F1 A1 FF FF FF FF FF FF FF','7F 2E 13','3'),       
                    ('2E F1 A1 FF FF FF FF FF FF FF FF FF','7F 2E 13','3'),
                    #('2E 34 4A','7F 2E 13','3'),  
                    #('2E 34 4A 00 00','7F 2E 13','3'),
                    ('2E F1 90','7F 2E 13','3'),
                    ('2E F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF','7F 2E 13','3'),                            
                    ('2E F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF','7F 2E 13','3'),
                ]
WriteDID_NRC31 =[   ('2E 34 02','7F 2E 31','3'), 
                    ('2E 21 02','7F 2E 31','3'), 
                    ('2E 21 00 00 00 00 00 00 00 00 00 00','7F 2E 31','3'), 
                    ('2E 21 02 00 00 00 00 00 00 00 00','7F 2E 31','3'),                                
                    ('2E F1 A2','7F 2E 31','3'),     
                    ('2E F1 A1 FF FF FF FF FF FF FF F6','7F 2E 31','3'),
                    ('2E F1 A1 00 FF FF FF FF FF FF F6','7F 2E 31','3'),
                    ('2E 34 4B','7F 2E 31','3'),  
                    ('2E 34 4B 00','7F 2E 31','3'),
                    ('2E F1 91','7F 2E 31','3'),                           
                    ('2E F1 92 FF FF FF FF FF FF FF FF FF','7F 2E 31','3'),
                ]
WriteDID_NRC7F = [  ('2E 34 01','7F 2E 7F','1'),                               # FactoryMode 生产模式
                    ('2E 21 00 00 00 00 00 00 00 00 00','7F 2E 7F','1'),          # Variant Coding 配置字
                    ('2E F1 A1 FF FF FF FF FF FF FF FF','7F 2E 7F','1'),       # Variant Coding 配置字擦除
                    #('2E 34 4A 00','7F 2E 7F','1'),                            # Lifecycle State Switch
                    ('2E F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF','7F 2E 7F','1'),  # VehicleIdentification(VIN) 车辆识别码
                 ]
WriteDID_NRC33 = [  ('2E 34 01','7F 2E 33','3'),                               # FactoryMode 生产模式
                    ('2E 21 00 00 00 00 00 00 00 00','7F 2E 33','3'),          # Variant Coding 配置字
                    ('2E F1 A1 FF FF FF FF FF FF FF FF','7F 2E 33','3'),       # Variant Coding 配置字擦除
                    #('2E 34 4A 00','7F 2E 33','3'),                            # Lifecycle State Switch
                    ('2E F1 90 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF','7F 2E 33','3'),  # VehicleIdentification(VIN) 车辆识别码
                 ]     

WriteDID_NRC10 = [ 
                    ('2E 21 00 10 CF 70 01 00 00 00 00'),              # Variant Coding 配置字
                    ('2E 21 00 10 CF 70 02 00 00 00 00'), 
                    ('2E 21 00 10 CF 70 03 00 00 00 00'), 
                ]

class Test_WriteDataById_Phy(object):

    ids = (f"{req}" for req, resp, DIDreq, DIDresp in WriteDID)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp, DIDreq, DIDresp', WriteDID, ids=ids)
    def test_WriteDataById_Extended_withPowerOff_Phy(self, action, Driver, req, resp, DIDreq, DIDresp):
        iStep = 1
        if req.find('2E 21 00') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        else:
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, DIDreq, DIDresp, 0, iStep, False)
    
        # 掉电重启后读取写入数据是否正常
        iStep = Driver['CAN'].restartMRR_Power(Driver, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, DIDreq, DIDresp, 1, iStep, False)


        if req.find('2E 34 01 01') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E 34 01 00", '6E 34 01', 3, iStep, True)

        if req.find('2E 21 00') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)

    
    ids = (f"{req}" for req, resp, DIDreq, DIDresp in WriteDID)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp, DIDreq, DIDresp', WriteDID, ids=ids)
    def test_WriteDataById_Extended_with1101_Phy(self, action, Driver, req, resp, DIDreq, DIDresp):
        iStep = 1
        if req.find('2E 21 00') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        else:
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, DIDreq, DIDresp, 0, iStep, False)

        # 1101重启读取信息是否保存
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "1101", "5101", 0, iStep, False)
        time.sleep(3)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, DIDreq, DIDresp, 1, iStep, False)
    
        if req.find('2E 34 01 01') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E 34 01 00", '6E 34 01', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 34 01", "62 34 01 00", 1, iStep, False)

        if req.find('2E 21 00') >= 0:
            Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)

        

    ids = (f"{req}" for req, resp, session in WriteDID_NRC13)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp,session', WriteDID_NRC13, ids=ids)
    def test_WriteDataById_NRC13_Extended_Phy(self, action, Driver, req, resp, session):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

    ids = (f"{req}" for req, resp, session in WriteDID_NRC31)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp,session', WriteDID_NRC31, ids=ids)
    def test_WriteDataById_NRC31_Extended_Phy(self, action, Driver, req, resp, session):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3,iStep, True)

    ids = (f"{req}" for req, resp, session in WriteDID_NRC7F)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp,session', WriteDID_NRC7F, ids=ids)
    def test_WriteDataById_NRC7F_Defalut_Phy(self, action, Driver, req, resp, session):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep)

    ids = (f"{req}" for req, resp, session in WriteDID_NRC33)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test {req} of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp,session', WriteDID_NRC33, ids=ids)
    def test_WriteDataById_NRC33_Extended_Phy(self, action, Driver, req, resp, session):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3,iStep)


    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test NRC22 of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_WriteDataById_NRC22_Extended_Phy(self, action, Driver):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E 21 00 10 A5 70 00 00 00 00 70', "6E 21 00", 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E 21 00 11 C3 70 01 03 00 00 1D', "7F 2E 22", 0, iStep, False)

    ids = (f"{req}" for req in WriteDID_NRC10)
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.title('Test NRC10 of Write data by identifier 0x2E')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req', WriteDID_NRC10, ids=ids)
    def test_WriteDataById_NRC10_Extended_Phy(self, action, Driver,req):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, "2E F1 A1 FF FF FF FF FF FF FF FF", '6E F1 A1', 3, iStep, True)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, "7F 2E 10", 0, iStep, False)



    